package yml;

import org.ho.yaml.Yaml;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * .
 *
 * @author sunhao(sunhao.java @ gmail.com)
 * @version 1.0 2018/1/9 下午10:31
 */
public class YmlTest {
    private static Person father = new Person();
    private static File dumpFile;

    static {
        try {
            dumpFile = new File(Thread.currentThread().getContextClassLoader().getResource("yml/test.yml").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        init();

        //write();
        read();
    }

    private static void write() throws FileNotFoundException {
        Yaml.dump(father, dumpFile);
    }

    private static void read() throws FileNotFoundException {
        Person person = Yaml.loadType(dumpFile, Person.class);
        System.out.println(person);
    }

    private static void init() {
        father.setName("simon.zhang");
        father.setAge(23);
        father.setSex("man");
        List<Person> children = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Person child = new Person();
            if (i % 2 == 0) {
                child.setSex("man");
                child.setName("mary" + i);
            } else {
                child.setSex("fatel");
                child.setName("simon" + i);
            }
            child.setAge(i);
            children.add(child);
        }
        father.setChildren(children);
    }
}
